<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button
      class="post-btn"
      @click="onAddComment"
    >发布</van-button>
  </div>
</template>

<script>
import {addComment} from '@/api/comment'

export default {
  name: 'CommentPost',
  props: ['target'],
  data () {
    return {
      message: ''
    }
  },
  methods: {
    async onAddComment() {
      console.log(8989, this.target)
      this.$toast.loading('发表评论中')
      const message = this.message.trim()

      if (!message.length) {
        return this.$toast('评论不能为空')
      }

      try {
        const {data} = await addComment({
          target: this.target || this.$route.params.articleId,
          content: message,
          art_id: this.target ? this.$route.params.articleId : null
        })

        this.$toast('发表评论成功')
        this.$emit('submitSuccess', data)
      } catch(err) {
        this.$toast('发表失败')
      }
     
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>

